<?php
namespace app\shop\model;
use app\common\model\CommonModel;
use app\common\model\Category;
use app\common\model\PurchaseOrder;
use app\common\model\Shop;
use think\Session;
use think\Db;

class PurchaseModel extends CommonModel
{
	/**
	* 添加商品信息
	*/
	public static function setPurchase($info)
	{
		$data['admin_id'] = Session::get('admin.admin_id');
		$data['shop_id'] = Session::get('admin.shop_id');
		$data['goods_name'] = $info['goods_name'];
		$data['category_name'] = $info['category_name'];
		$data['cat_name'] = $info['cat_name'];
		$data['brand_name'] = $info['brand_name'];
		$data['memory_name'] = $info['memory_name'];
		$data['attribute_name'] = $info['attribute_name'];
		$data['colour_name'] = $info['colour_name'];
		$data['marker'] = $info['marker'];
		$data['unit'] = $info['unit'];
		$data['number'] = $info['number'];
		$data['buying_price'] = $info['buying_price'];
		$data['total_price'] = $info['total_price'];
		$data['supplier_name'] = $info['supplier_name'];
		$data['remarks'] = $info['remarks'];
        $data['state'] = 1;
		$data['addtime'] = time();
		if(!(new PurchaseOrder())->setPurchase($data))
		{
			return back(config('state.STATUS_CODE_TWO'),config('state.STATUS_MSG_TWO'));
		}
		return back(config('state.STATUS_CODE_ONE'),config('state.STATUS_MSG_ONE'));
	}
	/**
	* 列表 
	*/
	public function getPurchaseList($info,$shop = false)
	{
		$list = (new PurchaseOrder())->getPurchaseList(self::is_where($info),10,$info['page']);
        if($shop === true)
        {
           $Shop = new Shop();
        }
       foreach($list['data'] as $i=>$vo)
        {
            $list['data'][$i]['state'] = self::isShow($vo['state']);
            $list['data'][$i]['addtime'] = date('Y-m-d',$vo['addtime']);
            if($shop === true)
            {
                if($vo['shop_id'] == 0)
                {
                   $list['data'][$i]['shop_id'] = '总部'; 
                }else{
                    $list['data'][$i]['shop_id'] = $Shop->shopField(['shop_id'=>$vo['shop_id']],'shop_name');
                }
            }
        }
		return $list;
	}
    /**
    * 详情
    */
    public function getPurchaseInfo($info)
    {
          $Purchase = PurchaseOrder::get($info['id']); 
          if($Purchase['shop_id'] == 0)
            {
               $Purchase['shop_id'] = '总部'; 
            }else{
                $Purchase['shop_id'] = (new Shop())->shopField(['shop_id'=>$Purchase['shop_id']],'shop_name');
            }
          $Purchase['states'] = self::isShow($Purchase['state']);
          return $Purchase;
    }
    /**
    * 修改状态
    */
    public function purState($info)
    {
        $where['pur_order_id'] = $info['id'];
        $data['state'] = $info['ty']==1?2:3;
        if((new PurchaseOrder())->updatePurchaseGoods($where,$data) === false)
        {
			return back(config('state.STATUS_CODE_TWO'),config('state.STATUS_MSG_TWO'));
		}
		return back(config('state.STATUS_CODE_ONE'),config('state.STATUS_MSG_ONE'));
    }
    /**
    * 状态
    */
    private function isShow($ty)
    {
        switch($ty)
        {
            case 0:return '待提交';break;
            case 1:return '待审核';break;
            case 2:return '已通过';break;
            case 3:return '未通过';break;
        }
    }
	/**
	* 条件
	*/
	private function is_where($info)
	{
		$where = [];
        /*产品属性*/
		if($info['attribute_name'] != '' && $info['attribute_name'])
		{
			$where['attribute_name'] = $info['attribute_name'];
		}
        /*产品品牌*/
		if($info['brand_name'] != '' && $info['brand_name'])
		{
			$where['brand_name'] = $info['brand_name'];
		}
        /*产品子分类*/
        $Category = new Category();
		if($info['cat_id'] != CommonModel::NUMBER_ZERO)
		{
			$where['cat_name'] = $Category->getCategoryField(['category_id'=>$info['cat_id']],'category_name');
		}
        /*产品分类*/
		if($info['category_id'] != CommonModel::NUMBER_ZERO)
		{
			$where['category_name'] = $Category->getCategoryField(['category_id'=>$info['category_id']],'category_name');
		}
        /*产品颜色*/
		if($info['colour_name'] != '' && $info['colour_name'])
		{
			$where['colour_name'] = $info['colour_name'];
		}
        /*产品内存*/
		if($info['memory_name'] != '' && $info['memory_name'])
		{
			$where['memory_name'] = $info['memory_name'];
		}
        /*关键字搜索*/
		if($info['sreach'])
		{
			$where['goods_name|supplier_name'] = ['like','%'.$info['sreach'].'%'];
		}
        /*时间区间*/
		if($info['start'] != '' || $info['end'] != '')
		{
			if($info['start'])
			{
				$where['addtime'][] = ['>',strtotime($info['start'])];
			}
			if($info['end'])
			{
				$where['addtime'][] = ['<',strtotime($info['end'])];
			}
			return $where;
		}
		
		if($info['shijian'] == CommonModel::NUMBER_ZERO)
		{
			$where['addtime'][] = ['>',mktime(0,0,0,date('m'),1,date('Y'))];
			$where['addtime'][] = ['<',mktime(23,59,59,date('m'),date('t'),date('Y'))];
		}else{
			$where['addtime'][] = ['>',mktime(0, 0, 0,date('m')-1,1,date('Y'))];
			$where['addtime'][] = ['<',mktime(0, 0, 0,date('m'),0,date('Y'))];
		}
		
		if($info['state'])
		{
			$where['state'] = $info['state'];
		}
		
		return $where;
	}
}
